﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace ImageEffect
{
    class Util
    {
        static public int Interpolate(int a, int b, double t)
        {
            return (int )((double )(b - a) * t) + a;
        }

        static public Color Interpolate(Color a, Color b, double t)
        {
            return Color.FromArgb(
                Interpolate(a.A, b.A, t),
                Interpolate(a.R, b.R, t),
                Interpolate(a.G, b.G, t),
                Interpolate(a.B, b.B, t)
            );
        }

        public delegate void RawBitmapDelegate(byte[] bmpData, int width, int height);

        static public void OnRawData(Bitmap bmp, RawBitmapDelegate rbd)
        {
            BitmapData bmpData = bmp.LockBits(
                new Rectangle(0, 0, bmp.Width, bmp.Height),
                ImageLockMode.ReadWrite,
                PixelFormat.Format32bppArgb
            );

            int bmpSize = bmpData.Stride * bmpData.Height;
            byte[] bmpBits = new byte[bmpSize];

            Marshal.Copy(bmpData.Scan0, bmpBits, 0, bmpSize);

            rbd(bmpBits, bmpData.Width, bmpData.Height);

            Marshal.Copy(bmpBits, 0, bmpData.Scan0, bmpSize);
            bmp.UnlockBits(bmpData);
        }

        static public double ToRadians(int a)
        {
            const double P = Math.PI / 180;
            return a * P;
        }
    }
}
